<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>INSPINIA | Basic Form</title>
	<link href="#springUrl('/static/css/bootstrap.min.css')" rel="stylesheet">
	<link href="#springUrl('/static/css/style.css')" rel="stylesheet">
</head>
<body class="white-bg">
<div class="ibox-content" style="width: 430px;">
    <form id="myForm" class="form-horizontal" autocomplete="off"  data-validator-option="{theme:'default'}">
        <div class="form-group"><label class="col-sm-2 control-label">商品名称</label>
            <div class="col-sm-10">
                    <input type="hidden" value="$!{banner.itemId}" name="itemId" id="itemId" >
                    <input type="text" class="form-control" readonly value="$!{banner.itemName}" name="itemName" id="itemName" >
                    <a class="btn" onclick="openItemSearch('itemId','itemName')">选择商品</a>
            </div>
        </div>
        <div class="form-group"><label class="col-sm-2 control-label">排序因子</label>
            <div class="col-sm-10">
                <input type="text"  class="form-control" name="sortFactor" />
            </div>
        </div>
        <div class="form-group"><label class="col-sm-2 control-label">轮播图片</label>
            <div class="col-sm-10">
                <input id="posterImage" type="file" style="display:none">
                <input name="posterImageAddr" type="hidden" value="$!{banner.posterImageAddr}">
                <a id="posterImageView">$!{banner.posterImageAddr}</a>
                <br/>
                <a class="btn" onclick="$('#posterImage').click();">选择图片文件</a>
                <a class="btn" onclick="removeImage('posterImageView','posterImageAddr')">清空</a>
            </div>
        </div>
        <div class="form-group">
            <div class="text-center">
                <button class="btn btn-primary" type="submit">提交</button>
            </div>
        </div>
    </form>
</div>
<script src="#springUrl('/static/js/jquery-2.1.1.js')"></script>
<script src="#springUrl('/static/plugins/nice-validator-0.8.1/jquery.validator.js?local=zh-CN')"></script>
<script src="#springUrl('/static/plugins/layer-v2.2/layer/layer.js')"></script>
<script src="#springUrl('/static/js/itemSelect.js')"></script>
<script type="text/javascript">
    /*$("#picture").change(function(){
        imgChange(event)
    });*/

    $("#posterImage").change(function(event){
        layer.load();
        var e = event;
        var type = e.target.files[0].type;//格式
        type = type.split("/")[1];
        var reader = new FileReader();
        reader.onload = (function (file) {
            return function (e) {
                var bate64 = this.result//这个就是base64的数据了
                bate64 =bate64.split(",")[1];
                $.ajax({
                    type: 'POST',
                    url: "http://h5.ciaotalking.com/upload/UploadServlet",
                    dataType: "json",
                    data:{"imgStr":bate64,"postfix":type},
                    success: function(data){
                        $("#posterImageView").html(data.content);
                        $("*[name='posterImageAddr']").val(data.content);
                        layer.closeAll('loading');
                    },
                    error: function(){
                        layer.closeAll('loading');
                    }
                });
            };
        })(e.target.files[0]);
        reader.readAsDataURL(e.target.files[0]);
    });

    function removeImage(aId,iName){
        var $a =  $("#"+aId);
        $a.html("");
        /*$("*[name="+iName+"]").val();
        if (aId == "posterImageView" && $("#posterImageAddrAddBtn").hasClass("OverFiveImage")){
            $("#infoImageAddrsAddBtn").removeClass("OverFiveImage");
            $("#infoImageAddrsAddBtn").show();
        }*/
    }

$("#myForm").validator({
    valid: function(form){
        var me = this;
        if($("#item").val()==''){
            layer.confirm('请先选择商品！', {btn: ['确定','取消']}, function () {return;},function () {return;});
        }
        // 提交表单之前，hold住表单，防止重复提交
        me.holdSubmit();
        $.ajax({
        	url: "#springUrl('/operate/banner/addBanner')",
            data: $(form).serialize(),
            type: "POST",
            success: function(data){
            	var d = JSON.parse(data);
                if(d.success && d.data){
                	window.parent.location.reload();
                	//当你在iframe页面关闭自身时
                	//var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
                	//parent.layer.close(index); //再执行关闭   
                } else {
                	//失败，提交表单成功后，释放hold，如果不释放hold，就变成了只能提交一次的表单
                    me.holdSubmit(false);
                }
            }
        });
    }
});
    $(function(){
        $.ajax({
            url:"#springUrl('/operate/banner/itemList')",
            type:"post",
            dataType:"json",
            success:function(data){
                /*console.log(data.itemName);*/
                var item = $("#item")
                $.each(data,function(i,n){
                    $("#item").attr("value", n.shopItem.id);
                    var textNode = document.createTextNode(n.shopItem.itemName);
                    var op = document.createElement("option");
                    op.setAttribute("value", n.shopItem.id);
                    $(op).append(textNode);
                    $(op).appendTo($("#item"));
                });

            }
        });
    });

function imgChange(e) {//上传图片
    var UploadServlet = "http://h5.ciaotalking.com/upload/UploadServlet";
    console.info(e.target.files[0]);//图片文件
    var type = e.target.files[0].type;//格式
    type = type.split("/")[1];
    console.log(type)
    var dom =$("input[id^='picture']")[0];
    //console.info(dom.value);//这个是文件的路径 C:\fakepath\icon (5).png
    //console.log(e.target.value);//这个也是文件的路径和上面的dom.value是一样的
    var reader = new FileReader();
    reader.onload = (function (file) {
        return function (e) {
            var bate64 = this.result//这个就是base64的数据了
            bate64 =bate64.split(",")[1];
            /!*$("#showImage")[0].src=this.result;*!/
            $.ajax({
                type: 'POST',
                url: UploadServlet,
                dataType: "json",
                data:{"imgStr":bate64,"postfix":type},
                success: function(data){
                    console.log(data.content);
                    $("#posterImageAddr").val(data.content);
                    $("#showImage").attr("src",data.content);
                },
                error: function(){

                }
            });
        };
    })(e.target.files[0]);
    reader.readAsDataURL(e.target.files[0]);
}

</script>
</body>
</html>
